<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
      @submit.native.prevent
    >
      <el-form-item label="设配名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入"
          clearable
          size="small"
          style="width: 240px"
          @change="handleQuery"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用状态" prop="status">
        <el-select v-model="queryParams.status" clearable @change="handleQuery">
          <el-option value="0" label="启用"></el-option>
          <el-option value="1" label="停用"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          plain
          @click="handleAdd"
          v-hasPermi="['setting:equipment:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain size="mini" @click="handleDelete" v-hasPermi="['setting:equipment:delete']"
          >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getTableRecords"
      ></right-toolbar>
    </el-row>
    <!-- 选中提示 -->
    <div class="choice-tips" v-if="multipleSelection.length > 0">
      <div style="display: flex; align-item: center">
        <i
          class="el-icon-warning"
          style="color: #078fea; font-size: 20px; margin-right: 5px"
        ></i>
        <span style="font-size: 14px"
          >已选择<span style="color: #078fea"
            >&nbsp;{{ multipleSelection.length }}&nbsp;</span
          >项</span
        >
      </div>
    </div>
    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="records"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="id" prop="eqId" v-if="false" />
      <el-table-column label="序号" type="index" width="100"></el-table-column>
      <el-table-column label="设备名称" prop="equipmentName" min-width="150" />
      <el-table-column label="关联组别" prop="contactName" min-width="150" />
      <el-table-column label="创建时间" prop="createTime" min-width="150" />
      <el-table-column label="排序" prop="equipmentSort" width="100" />
      <el-table-column label="停用/启用" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="'0'"
            :inactive-value="'1'"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        min-width="200"
        class-name="small-padding fixed-width"
      >
        <template scope="scope">
          <el-button
            v-show="scope.row.status == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['setting:equipment:edit']"
            >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-details"
            @click="details(scope.row)"
            v-hasPermi="['setting:equipment:detail']"
            >详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 10"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[10]"
      @pagination="getTableRecords"
    />
    <!-- 提交表单 -->
    <el-dialog
      :title="form.title"
      :visible.sync="formOpen"
      width="780px"
      :showClose="false"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        label-width="80px"
        inline
        :disabled="!formEnable"
        :rules="rules"
      >
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input
            v-model="form.equipmentName"
            class="form-inp"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="关联组别" prop="contactGroup">
          <el-select v-model="form.contactGroup" class="form-inp" clearable>
            <el-option
              v-for="(v, k) in gOptions"
              :key="k"
              :label="v.deptName"
              :value="v.deptId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="equipmentSort">
          <el-input-number
            class="form-inp"
            v-model="form.equipmentSort"
            controls-position="right"
            @change="numChange"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { groupOptions } from "@/api/system/dept.js";
import {
  addNewEquipment,
  getEquipmentRecords,
  deleteMultipleByIds,
  updateEquipment,
} from "@/api/system/equipment.js";
export default {
  data() {
    return {
      loading: false,
      // 表单是否可用
      formEnable: true,
      // 每页数量
      total: 10,
      showSearch: true,
      multipleSelection: [],
      // 提交表单
      formOpen: false,
      // 表格记录
      records: [],
      gOptions: [
        { value: 1, label: "组别1" },
        { value: 2, label: "组别2" },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptName: null,
        category: null,
      },
      // 表单
      form: {
        title: "新增表单",
        status: "0",
        equipmentSort: 1,
      },
      // 验证规则
      rules: {
        equipmentName: [
          { required: true, trigger: "change", message: "设备名称不能为空" },
        ],
        contactGroup: [
          { required: true, trigger: "change", message: "关联组别不能为空" },
        ],
      },
    };
  },
  created() {
    this.pageInit();
    this.getTableRecords();
  },
  methods: {
    // 初始化页面
    pageInit() {
      groupOptions().then((res) => {
        this.gOptions = res.data;
      });
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getTableRecords();
    },
    // 表单重置
    reset() {
      if (this.$refs.form) {
        this.$refs.form.resetFields();
      }
      this.formEnable = true;
      this.form = {
        title: "新增表单",
        status: "0",
        equipmentSort: 1,
      };
      this.formOpen = true;
    },
    // 新增按钮
    handleAdd() {
      this.reset();
      this.formOpen = true;
    },
    // 删除按钮
    handleDelete() {
      if (this.multipleSelection.length == 0) {
        this.$message.warning("请先选择待删除对象");
        return;
      }
      // 待删除集合
      const waitDeleteIds = [];
      for (var i = 0; i < this.multipleSelection.length; i++) {
        if (this.multipleSelection[i].status == "0") {
          this.$message.error("选中数据中有仍在启用状态的数据，无法删除");
          flag = true;
          return;
        }
        waitDeleteIds.push(this.multipleSelection[i].eqId);
      }
      this.$confirm(`确认删除该设备吗?`, "删除确认", {}).then(() => {
        this.loading = true;
        deleteMultipleByIds({ ids: waitDeleteIds }).then((res) => {
          this.loading = false;
          this.$message.success(res.msg);
          this.getTableRecords();
        });
      });
    },
    // 获取表格数据
    getTableRecords() {
      this.loading = true;
      getEquipmentRecords(this.queryParams).then((res) => {
        this.records = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 详情
    async details(row) {
      this.formEnable = false;
      this.form = row;
      this.form["title"] = "设备详情";
      this.formOpen = true;
    },
    // 修改
    handleUpdate(row) {
      this.tempRow = row;
      this.formEnable = true;
      this.form = row;
      this.form["title"] = "设备修改";
      this.formOpen = true;
    },
    // 停用启用
    handleStatusChange(row) {
      const temp = row.status; // 修改之后的状态
      row.status = temp == "1" ? "0" : "1";
      let confirmText = "";
      confirmText = row.status == "1" ? "停用" : "启用";
      this.$confirm(`确认${confirmText}该设备吗？`, "状态修改", {}).then(() => {
        const datas = { eqId: row.eqId, status: temp };
        this.loading = true;
        updateEquipment(datas).then((res) => {
          this.$message.success(res.msg);
          this.getTableRecords();
          this.loading = false;
        });
      });
    },
    // 表格多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 重置
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        deptName: null,
        category: null,
      };
      this.getTableRecords();
    },
    // 表单提交
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.eqId) {
            updateEquipment(this.form).then((res) => {
              this.$message.success(res.msg);
              this.formOpen = false;
              this.getTableRecords();
            });
          } else {
            addNewEquipment(this.form).then((res) => {
              this.$message.success(res.msg);
              this.formOpen = false;
              this.getTableRecords();
            });
          }
        }
      });
    },
    // 取消
    cancel() {
      this.formOpen = false;
      this.getTableRecords();
    },
    // 排序控件
    numChange(v) {
    },
  },
};
</script>

<style scoped>
.choice-tips {
  width: 100%;
  height: 40px;
  background-color: #e6f7ff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding-left: 10px;
  border-radius: 5px;
  border: 1px solid #bae7ff;
}

.form-inp {
  width: 250px;
}
</style>
